#!/bin/bash
# author:zgy
# 2023.4.7

# 脚本命令执行报错退出显示
set -e

# 获取输入参数
parameter=$1

#################版本对应脚本参数、init参数、start参数######################
# master 版本
master_parameter_init_start=master

# OpenHarmony-3.2-beta2 版本
OpenHarmony_3_2_beta2_parameter=OpenHarmony-3.2-beta2
OpenHarmony_3_2_beta2_init_start=OpenHarmony-3.2-Beta2

# OpenHarmony-3.2-beta2-Merge 版本
OpenHarmony_3_2_beta2_Merge_parameter_init=OpenHarmony-3.2-beta2-Merge
OpenHarmony_3_2_beta2_Merge_start=OpenHarmony-3.2-beta2

# OpenHarmony-3.2-Release 版本
OpenHarmony_3_2_Release_parameter_init_start=OpenHarmony-3.2-Release
#########################################################################

# 仓库地址
address_gitee=https://gitee.com/openharmony/manifest

# 版本相应参数
parameter_init=""
parameter_xml=""
parameter_start=""

# 显示菜单
function show_menu() {
  echo "*******************************************"
  echo "* Welcome to download OpenHarmony Code    *"
  echo "* Please Choice OpenHarmony SDK:          *"
  echo "* OpenHarmony Version                     *"
  echo "*   master                      prese 1.1 *"
  echo "*   OpenHarmony-3.2-beta2       press 1.2 *"
  echo "*   OpenHarmony-3.2-beta2-Merge press 1.3 *"
  echo "*   OpenHarmony-3.2-Release     press 1.4 *"
  echo "*******************************************"
  echo "if you don't want to download, you can input [no] to exit!"
  echo "please input parameter version: "

  read -r line
  parameter=$line
  check_parameter
}

# 显示下载信息
function show_download_information() {
  echo "you have been download OpenHarmony $parameter code"
  echo "url       : $address_gitee"
  echo "branch    : $parameter"
  echo "xml_name  : ${parameter_xml:3}"
  echo "code_path : $(pwd)"
  echo "init   cmd: repo init -u $address_gitee -b $parameter_init $parameter_xml --no-repo-verify"
  echo "sync   cmd: repo sync -c"
  echo "lfs    cmd: repo forall -c 'git lfs pull'"
  echo "set_br cmd: repo start $parameter_start --all"
}

# 创建并进入版本目录
function make_cd_directory() {
  if [ ! -e "$parameter" ]; then
    mkdir "$parameter"
    cd "$parameter"
  elif [ ! -e "$parameter"_temp ]; then
    mkdir "$parameter"_temp
    cd "$parameter"_temp
  else
    rm -rf "$parameter"_temp
    mkdir "$parameter"_temp
    cd "$parameter"_temp
  fi
}

function download() {
  echo "******************************Simulated download start******************************"
  echo "repo init -u $address_gitee -b $parameter_init $parameter_xml --no-repo-verify"
  echo "repo sync -c"
  echo "repo forall -c 'git lfs pull'"
  echo "repo start $parameter_start --all"
  echo "*******************************Simulated download end*******************************"
}

function check_parameter() {
  if [ "$parameter" == "$master_parameter_init_start" ]; then
    parameter_init=$master_parameter_init_start
    parameter_xml=""
    parameter_start=$master_parameter_init_start
  elif [ "$parameter" == "$OpenHarmony_3_2_beta2_parameter" ]; then
    parameter_init=$OpenHarmony_3_2_beta2_init_start
    parameter_xml=""
    parameter_start=$OpenHarmony_3_2_beta2_init_start
  elif [ "$parameter" == "$OpenHarmony_3_2_beta2_Merge_parameter_init" ]; then
    parameter_init=$OpenHarmony_3_2_beta2_Merge_parameter_init
    parameter_xml="-m thead_develop.xml"
    parameter_start=$OpenHarmony_3_2_beta2_Merge_start
  elif [ "$parameter" == "$OpenHarmony_3_2_Release_parameter_init_start" ]; then
    parameter_init=$OpenHarmony_3_2_Release_parameter_init_start
    parameter_xml="-m default.xml"
    parameter_start=$OpenHarmony_3_2_Release_parameter_init_start
  elif [ "$parameter" == "no" ]; then
    return 0
  else
    echo -e "\nproduct is not in product list, please recheck!!!"
    show_menu
    return 0
  fi
  make_cd_directory
  download
  show_download_information
}

function main() {
  if [ ! "$parameter" ]; then
    show_menu
  else
    check_parameter
  fi
}

main
